<?
class Control_Banner extends QUI_Control_Abstract{

    static protected $groupedBanners;

	function render(){
        if(empty($this->position)) throw new Exception('banner position invalid');

        if(self::$groupedBanners === null){
            self::$groupedBanners = Banner::findByActive(1)->order('sort')->getAll()->toHashmap('position', null, true);
        }

        if(isset(self::$groupedBanners[$this->position])){
            $bs = self::$groupedBanners[$this->position];
        }else{
            // this position has no record
            return '';
        }

        $out = '<div id="'.$this->id().'">';
        foreach($bs as $b){
            $path = QContext::instance()->baseDir().'img/banner/'.$b->img;
            if($b->link){
                $out .= '<a href="'.$b->link.'" target="'.$b->link_target.'"><img src="'.$path.'" title="'.$b->alt.'" alt="'.$this->id().'" /></a>';
            }else{
                $out .= '<img src="'.$path.'" title="'.$b->alt.'" alt="'.$this->id().'" />';
            }
        }
        $out .= '</div>';

        return $out;
	}
}